<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Title</title>
  </head>
  <body>
    <script>
      // 复杂工厂模式

      // 父类：是一个抽象类,不可以直接实例
      // 子类：直接实例自己的方法
      function BicycleShop(name, method) {
        this.name = name;
        this.method = function () {
          return this.name;
        };
      }

      BicycleShop.prototype = {
        constructor: BicycleShop,
        sellBicycle: function () {
          let bicycle = this.createBicycle();
          bicycle.a();
          bicycle.b();
          return bicycle;
        },
        createBicycle: function () {
          throw new Error("父类不能实例化，需要子类实例");
        },
      };

      // 原型链继承
      //   BicycleChild.prototype = new BicycleShop();

      function BicycleChild(name) {
        // 构造函数继承
        BicycleShop.call(this.name);
        this.name = name;
      }

      BicycleChild.prototype.createBicycle = function () {
        var a = function () {
          console.log("执行a方法");
        };

        var b = function () {
          console.log("执行b方法");
        };
        return { a, b };
      };

      var bicycleChild = new BicycleChild("凤凰牌自行车");
      console.log(bicycleChild);
      console.log(bicycleChild.createBicycle());
      //   console.log(bicycleChild.sellBicycle());
      var bicycleShop = new BicycleShop("bilibili");
      console.log(bicycleShop.sellBicycle());
    </script>
  </body>
</html>
